package org.strongme.modules.project.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.strongme.common.web.BaseController;
import org.strongme.modules.project.entity.Project;
import org.strongme.modules.project.entity.SubProject;
import org.strongme.modules.project.service.SubProjectService;
import org.strongme.util.StringUtils;

import com.google.common.collect.Lists;

@Controller
@RequestMapping("project/subProject")
public class SubProjectController extends BaseController{
	
	@Autowired
	private SubProjectService subProjectService;
	
	@ModelAttribute("subProject")
	public SubProject get(@RequestParam(required=false) String id, @RequestParam(required=false)String projectId) {
		SubProject subProject = null;
		if (StringUtils.isNotBlank(id)){
			subProject = subProjectService.get(new SubProject(id));
		}else if(StringUtils.isNoneBlank(projectId)) {
			subProject = new SubProject();
			subProject.setProject(new Project(projectId));
		}else{
			subProject = new SubProject();
		}
		return subProject;
	}
	
	@RequestMapping(value={"index",""})
	public String Index() {
		return "modules/project/subProject/subProjectIndex.jsp";
	}
	
	@RequestMapping(value = {"list"})
	public String list(SubProject subProject, HttpServletRequest request, HttpServletResponse response, Model model) {
        model.addAttribute("list", subProjectService.findList(subProject));
		return "modules/project/subProject/subProjectList.jsp";
	}
	
	@RequestMapping(value = "form")
	public String form(SubProject subProject, Model model) {
		model.addAttribute("subProject", subProject);
		return "modules/project/subProject/subProjectForm.jsp";
	}
	
	@RequestMapping(value = "save")
	public String save(SubProject subProject, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, subProject)){
			return form(subProject, model);
		}
		subProjectService.save(subProject);
		addMessage(redirectAttributes, "保存单位工程'" + subProject.getName() + "'成功");
		return "redirect:/project/subProject/list?subProject.project.id="+subProject.getProject().getId();
	}
	
	@RequestMapping(value = "delete")
	public String delete(SubProject subProject, RedirectAttributes redirectAttributes) {
		subProjectService.delete(subProject);
		addMessage(redirectAttributes, "删除单位工程成功");
		return "redirect:/project/subProject/list";
	}
	
	/**
	 * 通过工程ID获取单位工程
	 * @param projectId
	 * @return
	 */
	@RequestMapping(value= "listSubByPro", method=RequestMethod.POST)
	@ResponseBody
	public List<SubProject> listSubProjectByProject(@RequestParam String projectId) {
		if(projectId==null || "".equals(projectId)) {
			return Lists.newArrayList();
		}
		List<SubProject> result = subProjectService.findListByProId(projectId);
		return result;
	}
}
